﻿using System;
using System.Collections.Generic;

namespace PKTools
{
    public static class ObjectExtender
    {
        public static bool In<T>(this T obj, params T[] objs)
        {
            foreach (var e in objs)
                if (obj.Equals(e))
                    return true;

            return false;
        }

        public static string ToDetailString(this DateTime value)
        {
            return value.ToString("yy-MM-dd HH:mm:ss.fff");
        }

        public static string ToDetailString(this DateTime? value)
        {
            if (value == null)
                return "<null>";

            return ToDetailString(value.Value);
        }

        public static int SafeToInt(this string value, int defaultValue)
        {
            int
                n;

            if (Int32.TryParse(value, out n))
                return n;

            return defaultValue;
        }

        public static KeyValuePair<TKey, TValue> Pair<TKey, TValue>(this TKey key, TValue value)
        {
            return new KeyValuePair<TKey, TValue>(key, value);
        }

        public static T IsNull<TValue, T>(this TValue value, Func<TValue, T> notNullF, T nullValue)
        {
            if (value != null)
                return notNullF(value);

            return nullValue;
        }
    }
}
